﻿param ([Switch]$sendEmail , [string]$OneOffEmailAddr = "default")
cls
#$DebugPreference = "SilentlyContinue"  #This is default
$DebugPreference = "Continue"  #This causes more output
#$DebugPreference = "Inquire"  #This causes output and queries to continue or stop
#$DebugPreference = "Stop"  #This causes output and stops

## This script produces a list of account forwarding mail.  It also lists the forward-to address.
## David Ray - 9/21/2010
Write-Debug "Starting Report Script"
Write-Debug "Setting configuration variables"

## Setup Variables
#Enter addresses in array form. i.e. @("name1@dom1.com","name2@dom2.com") if none use @("")
$defaultTo = @("Rick_M@dell.com","bill_branson@dell.com")
$defaultCc = @("david_f_ray@dell.com")
$defaultBcc = @("")
$defaultFrom = "ExchangeOp@ps.net"
$SMTPserver = "smtp-relay.chw.edu"
$subject = "CHW Monthly Forwarders Report"
$emailMessage = "The attached file reports all mailboxes being forwarded and the addresses forwarded to."
$reportName = "CHWForwardingReport" + $(Get-Date -Format "yyMMdd")
$reportPath = "E:\Reports\CHWForwardingReport"
$emailSubject = "CHW Monthly Forwarders Report"
$emailBody = "The attached file reports all mail-enabled users who are delivering to a alternate address."

#Load up the Report Library Functions.  ReportLibrary.ps1 must be in the same dir as the report script
Write-Debug "Load Report Libary Functions"
#. ((Split-Path -Parent (Resolve-Path -Path $MyInvocation.InvocationName)) + "\ReportLibrary.ps1")
$ScriptName = $MyInvocation.MyCommand.Name
$ScriptDir = Split-Path $MyInvocation.MyCommand.Path
. "$ScriptDir\ReportLibrary.ps1"

#######
## Here is the main data gathering portion of the script
Write-Debug "Running command to get data"

$forwarding = Get-Mailbox -ResultSize:Unlimited -errorAction SilentlyContinue | Where-Object {$_.forwardingAddress -ne $Null}

$fwds = @()

## Loop through basic info populating ReportObject
Write-Debug "Entering loop to build data list"
ForEach ($user in $forwarding) {
	$fwd = New-Object PSObject
	$fwd | Add-Member -MemberType NoteProperty -Name DisplayName -Value $user.DisplayName
	$fwd | Add-Member -MemberType NoteProperty -Name Alias	-Value $user.Alias
	$fwd | Add-Member -MemberType NoteProperty -Name SamAccountName $user.SamAccountName
	$ForwardDestination = $user.ForwardingAddress | Get-Recipient | Select-Object PrimarySmtpAddress
	$ForwardAddress = $ForwardDestination.PrimarySmtpAddress.Local + "@" + $ForwardDestination.PrimarySmtpAddress.Domain
	$fwd | Add-Member -MemberType NoteProperty -Name ForwardDestination -Value $ForwardAddress
	$fwds += $fwd
}
Write-Debug "Exiting data building loop"
## Output CSV of the information
Write-Debug "Saving CSV to $($reportPath)\$($reportName).csv"
$fwds | Export-Csv -NoTypeInformation "$($reportPath)\$($reportName).csv"


##
## Save all this out to Excel and prettify it some.
##

## Excel constants that will help us
Write-Debug "Call Excel Var Function"
Set-ExcelVars

# Create an instance of Excel
Write-Debug "Call function to open Excel with CSV"
Open-ExcelCSV $reportPath $reportName

# Add a little formatting to the column header
Write-Debug "Call function to format header"
Set-ExcelHeaderFormat

# Make Columns wide enough for content
Write-Debug "Calling function to autofit columns"
Set-ExcelAutofitColumns

#Save the Excel File. Use format $xlOpenXMLWorkbook for normal xlsx and $xlExcel12 for xlsb
Write-Debug "Calling function to save file"
Save-Excel $reportPath $reportName $xlOpenXMLWorkbook
#Save-Excel makes available $reportFullName for sending

## Send Mail
if ($sendEmail.IsPresent) {
	Write-Debug "Calling send email function"
	Send-Email $OneOffEmailAddr $defaultTo $defaultCc $defaultBcc $defaultFrom `
					$subject $emailbody $reportFullName $SMTPserver
}
Write-Debug "End of Report script"